/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ManageUsers.java
 *
 * Created on 10/04/2011, 14:30:19
 */

package GUI;

import Business.FrameHandler;
import Domain.DataManager;
import Domain.UserClient.ButtonsFunctions;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Iterator;
import java.util.Vector;
import objects.User;
import javax.swing.DefaultListModel;
import objects.Frm;

/**
 *
 * @author Tal Cohen
 */
public class ManageUsers extends javax.swing.JFrame {

    private Vector<User>_usersVec;
    /** Creates new form ManageUsers */
    public ManageUsers(Vector<User> users) {
        initComponents();

        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        setLocation(screenWidth / 4, screenHeight / 4);

        _usersVec = new Vector<User>();
        //adding all users except the user that is logged in!
        for (Iterator<User> it = users.iterator(); it.hasNext(); ) {
            User tUserName = it.next();  // No downcasting required.
            if(!ButtonsFunctions.getButtonsFunctions().getCurrUser().equals(tUserName.getUserName()) )
                _usersVec.addElement(tUserName);
            }

          DefaultListModel usersListModel = new DefaultListModel();
         for (Iterator<User> it = _usersVec.iterator(); it.hasNext(); ) {
            User tUserName = it.next();  // No downcasting required.
            usersListModel.addElement(tUserName.getUserName());
        }
        this.usersList.setModel(usersListModel);
        errorLabel.setVisible(false);
    }

   /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        usersList = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        promoteBut = new javax.swing.JButton();
        removeBut = new javax.swing.JButton();
        back = new javax.swing.JButton();
        errorLabel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        userDetailsTextArea = new javax.swing.JTextArea();
        userDetailsLable = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Manage Users");

        usersList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                usersListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(usersList);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setText("Select a user, and then click on the operation:");

        promoteBut.setText("Promote To Manger");
        promoteBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                promoteButActionPerformed(evt);
            }
        });

        removeBut.setText("Remove User");
        removeBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButActionPerformed(evt);
            }
        });

        back.setText("Back");
        back.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backActionPerformed(evt);
            }
        });

        errorLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        errorLabel.setForeground(new java.awt.Color(255, 51, 51));
        errorLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        errorLabel.setText("errors will be displayed here");

        userDetailsTextArea.setColumns(20);
        userDetailsTextArea.setRows(5);
        jScrollPane2.setViewportView(userDetailsTextArea);

        userDetailsLable.setText("User Details :");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(back))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(57, 57, 57)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(16, 16, 16)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(removeBut)
                                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(promoteBut)))
                                    .addComponent(errorLabel)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(33, 33, 33)
                                        .addComponent(userDetailsLable)))))))
                .addContainerGap(80, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(53, 53, 53)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                        .addComponent(back))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(userDetailsLable)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(promoteBut)
                        .addGap(18, 18, 18)
                        .addComponent(removeBut)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(errorLabel)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void promoteButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_promoteButActionPerformed
        ButtonsFunctions.getButtonsFunctions().processChangeUserToManager((String)usersList.getSelectedValue());
        if(DataManager.getDataManager().getSuccessOrFailure() == DataManager.FAILURE)
                return;
        ButtonsFunctions.getButtonsFunctions().processGetUsers();
       Vector<User> users = DataManager.getDataManager().getVectorUser();
        FrameHandler.getFrameHandler().changeCurrentFrame(new ManageUsers(users));
    }//GEN-LAST:event_promoteButActionPerformed

    private void backActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backActionPerformed
        ButtonsFunctions.getButtonsFunctions().processGetForums();
        Vector<Frm> forums = DataManager.getDataManager().getVectorForums();
        int type = DataManager.getDataManager().getCurrTypeOfUser();
        FrameHandler.getFrameHandler().changeCurrentFrame(new ChooseForum(forums, type));
    }//GEN-LAST:event_backActionPerformed

    private void removeButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButActionPerformed
        ButtonsFunctions.getButtonsFunctions().processUnregister((String)usersList.getSelectedValue());
        if(DataManager.getDataManager().getSuccessOrFailure() == DataManager.FAILURE)
                return;
        ButtonsFunctions.getButtonsFunctions().processRemoveUser((String)usersList.getSelectedValue());
        if(DataManager.getDataManager().getSuccessOrFailure() == DataManager.FAILURE)
                return;

        ButtonsFunctions.getButtonsFunctions().processGetUsers();
       Vector<User> users = DataManager.getDataManager().getVectorUser();
        FrameHandler.getFrameHandler().changeCurrentFrame(new ManageUsers(users));
    }//GEN-LAST:event_removeButActionPerformed

    private void usersListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_usersListValueChanged
       String type;
        if (_usersVec.elementAt(usersList.getSelectedIndex()).getType().equals(User.Type.MANAGER)){
           type = "Manager";
       }
       else{
           type = "User";
       }
        userDetailsTextArea.setText("Type Of User: "+type);
    }//GEN-LAST:event_usersListValueChanged

    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton back;
    private javax.swing.JLabel errorLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton promoteBut;
    private javax.swing.JButton removeBut;
    private javax.swing.JLabel userDetailsLable;
    private javax.swing.JTextArea userDetailsTextArea;
    private javax.swing.JList usersList;
    // End of variables declaration//GEN-END:variables

}
